package leetcode.part3;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class S242 {
    public boolean isAnagram(String s, String t) {
        Map<String,Integer> map=new HashMap<>();
        String[] s1 = s.split("");
        String[] t1 = t.split("");
        for (String e : s1) {
            map.computeIfPresent(e,(key,val)->val+1);
            map.putIfAbsent(e,1);
        }
        for (String e : t1) {
            if (map.containsKey(e)){
                Integer i = map.get(e);
                if (i==1) map.remove(e);
                else map.compute(e,(key,val)->val-1);
            }else {
                return false;
            }
        }
        return map.isEmpty();
    }

    @Test
    public void test(){
        String s = "anagram", t = "nagaram";
        String s1 = "rat", t1 = "car";
        System.out.println(isAnagram(s, t));
        System.out.println(isAnagram(s1,t1));
    }
}
